var Observable;

/***************************************************************
 *
 *   Observable
 *
 ***************************************************************/
(Observable = function () {}).prototype = {
    listen: function (type, method, scope, context) {
        var listeners, handlers, scope;
        if (!(listeners = this.listeners)) {
            listeners = this.listeners = {};
        }
        if (!(handlers = listeners[type])) {
            handlers = listeners[type] = [];
        }
        scope = (scope ? scope : window);
        handlers.push({
            method: method,
            scope: scope,
            context: (context ? context : scope)
        });
    },
    fireEvent: function (type, data, context) {
        var listeners, handlers, i, n, handler, scope;
        if (!(listeners = this.listeners)) {
            return;
        }
        if (!(handlers = listeners[type])) {
            return;
        }
        for (i = 0, n = handlers.length; i < n; i++) {
            handler = handlers[i];
            if (typeof (context) !== "undefined" && context !== handler.context) continue;
            if (handler.method.call(
                handler.scope, this, type, data
            ) === false) {
                return false;
            }
        }
        return true;
    }
};

function pad(num) {
    var pad='00';
    return (pad+num).slice(-pad.length);
}

function getFormatedDate(cTime) {
    if (cTime instanceof Date) {
        return cTime.getFullYear() + '-' + pad(cTime.getMonth() + 1) + '-' + pad(cTime.getDate());
    } else {
        return cTime.year + '-' + cTime.month + '-' + cTime.date;
    }
}

function getTasklistTitle(title) {
    var tasklist_color_reg = /@color:#[A-Za-z0-9]{6}/g;
    return title.replace(tasklist_color_reg, "");
}

function getTasklistColor(title) {
    var tasklist_color_reg = /@color:#[A-Za-z0-9]{6}/g;
    var replace_color = tasklist_color_reg.exec(title);
    var str_replace_color = "";
    if(replace_color) {
        str_replace_color = replace_color[0].replace(/@color:#/, "");
    }

    return str_replace_color;
}

function invert(rgb) {
    rgb = [].slice.call(arguments).join(",").replace(/rgb\(|\)|rgba\(|\)|\s/gi, '').split(',');
    for (var i = 0; i < rgb.length; i++) rgb[i] = (i === 3 ? 1 : 255) - rgb[i];
    return "rgb(" + rgb.join(", ") + ")";
}

function doubleclick(el, evt, onsingle, ondouble) {
    if (el.getAttribute("data-dblclick") == null) {
        el.setAttribute("data-dblclick", 1);
        setTimeout(function () {
            if (el.getAttribute("data-dblclick") == 1) {
                onsingle();
            }
            el.removeAttribute("data-dblclick");
        }, 300);
    } else {
        el.removeAttribute("data-dblclick");
        ondouble();
    }
    evt.stopPropagation();
}

function createEditableLabel(title, update_callback) {

    var editableLabel = document.createElement('input');
    editableLabel.className = "editableLabel";
    editableLabel.style.readOnly = true;
    editableLabel.type = "text";
    editableLabel.value = title;
    editableLabel.ondblclick = toEditablebox;
    editableLabel.onmouseout = toUneditablebox;
    editableLabel.onchange = update_callback;
    editableLabel.onfocus = function () { this.blur(); };

    return editableLabel;
}

function toEditablebox(evt) {
    evt.stopPropagation();
    this.style.readOnly = false;
    this.onfocus = function () {};
    this.contentEditable = true;
    this.focus();
}

function toUneditablebox(evt) {
    evt.stopPropagation();
    this.style.readOnly = true;
    this.onfocus = function () {
        this.blur();
    };
    this.contentEditable = false;
    this.blur();
};

